package com.hermes.action.login;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.BaseAction;
import com.hermes.common.util.ParameterParserUtil;


/**
* 
* 用户登录的跳转
* 
*/

public class LoginAction extends BaseAction{

	private static final long serialVersionUID = -2841153430630242279L;
		
	private static Logger log = Logger.getLogger(LoginAction.class);
			
	private UserService userService;

	private String name;


		public String execute() {
			
						String redirectUrl = ParameterParserUtil.getParameter("redirectUrl");
						log.error("-------------------------------");
						log.error("-------"+redirectUrl);
						String username = ParameterParserUtil.getParameter("username");						
						String password = ParameterParserUtil.getParameter("password");
						log.error("password:"+password);
						log.error("username:"+username);
						UserResultDO userResultDO = userService.getUserInfoByUserName(username);
						
						if(userResultDO.isSuccess()){
							if(userResultDO.getModule()!=null && userResultDO.getModule().getUserPassword().endsWith(password)){
								setName(username);
								session.put("userName", username);
								session.put("userId", userResultDO.getModule().getUserId());
						        HttpServletResponse response = ServletActionContext.getResponse();
						        try {
						        	redirectUrl = URLDecoder.decode(redirectUrl, "GBK");
									response.sendRedirect(redirectUrl);
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								
								return SUCCESS;
							}
						}				
						return ERROR;	
		}
		
		public UserService getUserService() {
			return userService;
		}
		public void setUserService(UserService userService) {
			this.userService = userService;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
			
}